package ngames.com.sushitv.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.core.AppModel;
import ngames.com.sushitv.core.AppModelImpl;
import ngames.com.sushitv.tools.StackManager;
import ngames.com.sushitv.view.DialogAppLoading;

/**
 * Created by Administrator on 2016/1/7.
 */
public class BaseActivity extends Activity {
    protected AppApplication mAppApplication;
    protected AppModelImpl mAppModel;
    protected DialogAppLoading mDialogLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getAppModel().setCurrentActivity(this);
        StackManager.getStackManager().pushActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAppModel().setCurrentActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        StackManager.getStackManager().popActivity(this);
    }

    public void onTitleBack(View v) {
        finish();
    }

    public void setTitle(int resid) {
//        ((TextView) findViewById(R.id.txv_title)).setText(resid);
    }

    public AppApplication getAppApplication() {
        if (mAppApplication == null) {
            mAppApplication = (AppApplication) getApplication();
        }
        return mAppApplication;
    }

    protected void showProgress(boolean show) {
        if (show) {
            if (mDialogLoading == null) {
                mDialogLoading = new DialogAppLoading(this);
            }
            if (!mDialogLoading.isShowing()) {
                mDialogLoading.show();
            }
        } else {
            if (mDialogLoading != null && mDialogLoading.isShowing()) {
                mDialogLoading.cancel();
            }
        }
    }

    public AppModel getAppModel(){
        if (mAppModel == null || mAppApplication == null) {
            mAppModel = (AppModelImpl) getAppApplication().getAppModel();
        }
        return mAppModel;
    }
}
